package com.lzxuni.modules.dataAcquisition;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcess;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcessIndex;
import com.lzxuni.modules.dataAcquisition.entity.ProductIndex;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessIndexService;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessService;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import com.lzxuni.modules.dataAcquisition.service.ProductIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 追溯链条-环节-过程-指标 前端控制器
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@RestController
@RequestMapping("/LR_DataAcquisition/ChainLinkProcessIndex")
public class ChainLinkProcessIndexController extends BaseController {

    @Autowired
    private ChainLinkProcessService chainLinkProcessService;

    @Autowired
    private ChainLinkProcessIndexService chainLinkProcessIndexService;

    @Autowired
    private ProductIndexService productIndexService;

    @RequestMapping("/Index")
    @SysLog(categoryId = 2, module = "追溯链条-环节-过程-指标", operateType = "访问")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/ChainLinkProcessIndex/index");
        return mv;
    }

    @RequestMapping("/GetList")
    public Object GetList(String pagination, ChainLinkProcessIndex chainLinkProcessIndex, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        chainLinkProcessIndex.setIndexName(Keyword);
        PageData pageData = getPageData(chainLinkProcessIndexService.queryPage(pageParameter, chainLinkProcessIndex));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/Form")
    public ModelAndView insert(String parentId, String id) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/ChainLinkProcessIndex/form");
        ChainLinkProcessIndex index = new ChainLinkProcessIndex();
        if (StringUtils.isNotEmpty(id)) {
            index = chainLinkProcessIndexService.getById(id);
            mv.addObject("parentId", index.getParentId());
        }else{
            mv.addObject("parentId", parentId);
            Integer i = chainLinkProcessIndexService.count(new QueryWrapper<ChainLinkProcessIndex>().eq("parent_id", parentId));
            if (i == 0) {
                String parentCode = chainLinkProcessService.getById(parentId).getProcessCode();
                index.setIndexCode(parentCode + "01");
            }else {
                List<ChainLinkProcessIndex> indexs = chainLinkProcessIndexService.list(new QueryWrapper<ChainLinkProcessIndex>().eq("parent_id", parentId).orderByDesc("create_time"));
                int c = Integer.parseInt(indexs.get(0).getIndexCode());
                c++;
                index.setIndexCode(String.valueOf(c));
            }
        }
        mv.addObject("index", index);
        return mv;
    }

    @RequestMapping("/SaveForm")
    @SysLog(categoryId = 3, module = "追溯链条-环节-过程-指标", operateType = "增/改")
    public Object insertDo(HttpServletRequest request, String keyValue, ChainLinkProcessIndex chainLinkProcessIndex) throws Exception {
        chainLinkProcessIndex.setParentId(chainLinkProcessIndex.getProcessId());
        if (StringUtils.isEmpty(keyValue)) {
            chainLinkProcessIndex.setCreateTime(new Date());
            chainLinkProcessIndexService.save(chainLinkProcessIndex);
            return R.ok("保存成功");
        } else {
            ChainLinkProcessIndex chainLinkProcessIndexHistory = chainLinkProcessIndexService.getById(keyValue);
            if(!chainLinkProcessIndexHistory.getCollectionType().equals(chainLinkProcessIndex.getCollectionType())){
                chainLinkProcessIndex.setCollectionType(chainLinkProcessIndexHistory.getCollectionType());
            }
            chainLinkProcessIndex.setId(keyValue);
            Boolean bl = chainLinkProcessIndexService.updateById(chainLinkProcessIndex);
            if(bl){
                List<ProductIndex> productIndices = productIndexService.list(new QueryWrapper<ProductIndex>().eq("index_id", keyValue));
                for (ProductIndex productIndex : productIndices) {
                    productIndex.setIndexValue(chainLinkProcessIndex.getIndexName());
                    productIndex.setCollectionType(chainLinkProcessIndex.getCollectionType());
                    productIndexService.updateById(productIndex);
                }
            }
            return R.ok("修改成功");
        }
    }

    @RequestMapping("/DeleteForm")
    @SysLog(categoryId = 3, module = "追溯链条-环节-过程-指标", operateType = "删除")
    public Object delete(String keyValue) {
        chainLinkProcessIndexService.removeById(keyValue);
        return R.ok("删除成功");
    }

}

